﻿using System;
using System.Linq;
using System.Web.UI.WebControls;

namespace RaceList.Admin.Relay
{
    public partial class CycleList : System.Web.UI.Page
    {
        ipowdb_suibian_webDataContext db = ConnHelper.ipowdb_suibian_web();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Data_Bind();
            }
        }

        protected void Data_Bind()
        {
            var list = db.Activity_Cycle
                .Where(s => s.isDel == false)
                .OrderByDescending(s => s.addTime)
                .Select(s => new { s.Id, s.title, s.addTime, s.status, s.startDate, s.endDate });
            gvList.DataSource = list;
            gvList.DataBind();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string message = string.Empty;
            int? ID = Convert.ToInt32(e.CommandArgument.ToString());
            Activity_Cycle cycle = db.Activity_Cycle.SingleOrDefault(a => a.Id == ID);

            if (cycle == null)
            {
                StringHelper.Alert(this, "此信息不存在或可能被删除，请您刷新本页面后继续操作！", "#");
                return;
            }
            else
            {
                try
                {
                    switch (e.CommandName)
                    {
                        case "btnDel":
                            cycle.isDel = true;
                            cycle.delTime = DateTime.Now;
                            message = "删除";
                            break;
                        case "CheckStatus":
                            cycle.status = cycle.status ? false : true;
                            message = "发布";
                            break;
                    }
                    db.SubmitChanges();
                    this.Message.Text = "操作成功";
                }
                catch (Exception ex)
                {
                    StringHelper.Alert(this, "操作失败！", string.Empty);
                    this.Message.Text = "操作失败，失败原因：" + ex.Message + "！详细原因请查看错误日志。";
                }
                Data_Bind();
            }
        }

        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#EFF3FB'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='White'");
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            Data_Bind();
        }
    }
}